package com.eascs.admin.o2o.loan.notify.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eascs.admin.o2o.finance.entity.O2oFinanceFlowEntity;
import com.eascs.admin.o2o.finance.service.O2oFinanceFlowService;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.notify.O2OMsgNotifyService;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.workflow.common.entity.wf.ProcessTaskMain;
import com.eascs.workflow.common.entity.wf.ProcessTemplate;
import com.eascs.workflow.common.vo.ModelResult;
import com.eascs.workflow.common.vo.WFNotifyParam;

/**
 * 财务审核流程
 */
@Service
public class CWMsgNotifyServiceImpl implements O2OMsgNotifyService {
	private static Logger log = LoggerFactory.getLogger(CWMsgNotifyServiceImpl.class);
	
	@Autowired
	private O2oFinanceFlowService financeFlowService;
	
	@Override
	public ModelResult<O2oLoanapplyEntity> notify(WFNotifyParam notifyParam, O2oLoanapplyEntity loanapply){
		ModelResult<O2oLoanapplyEntity> modelResult =new ModelResult<>();
		log.info("启动财务审核流程通知:[{}]", StatusContexts.WFTYPE_FINANCE);
		financeFlow(notifyParam, loanapply);
		modelResult.withModel(loanapply);
		return modelResult;
	}

	@Override
	public List<String> getServiceName() {
		ArrayList<String> list = new ArrayList<String>();
		list.add(ProcessTemplate.PROECESS_TYPE_FINANCE_AUDIT);
		return list;
	}

	@Override
	public Boolean checkReturn() {
		return Boolean.TRUE;
	}
	

	/**
	 * 财务流程
	 * 
	 * @param notifyParam
	 * @param loanapplyEntity
	 * @return
	 */
	private ModelResult<Boolean> financeFlow(WFNotifyParam notifyParam, O2oLoanapplyEntity loanapplyEntity) {
		if (notifyParam.getProcessStatus() == ProcessTaskMain.PROECESS_STATUS_PASS) {
			log.info("财务审核通过：billcode【{}】", loanapplyEntity.getBillCode());
			O2oFinanceFlowEntity entity = financeFlowService.getByLoanApplyno(notifyParam.getReferId());
			entity.setFlowStatus(StatusContexts.O2O_BANK_CLO);
			entity.setEndTime(new Date());
			int i = financeFlowService.update(entity);
			if (i > 0) {
				return new ModelResult<Boolean>().withModel(Boolean.TRUE);
			} else {
				log.info("更新O2oFinanceFlowEntity财务审核失败:[{}]", entity.getBillCode());
				return new ModelResult<>("workflow.notify.insert.error", "财务审核流程通过通知失败");
			}
		} else if (notifyParam.getProcessStatus() == ProcessTaskMain.PROECESS_STATUS_TURNED_DOWN) {
			log.info("财务审核驳回：【{}】", loanapplyEntity.getBillCode());
			O2oFinanceFlowEntity entity = financeFlowService.getByLoanApplyno(notifyParam.getReferId());
			entity.setFlowStatus(StatusContexts.O2O_BANK_NEW);
			int i = financeFlowService.update(entity);
			if (i > 0) {
				return new ModelResult<Boolean>().withModel(Boolean.TRUE);
			} else {
				log.info("更新O2oFinanceFlowEntity财务审核失败:[{}]", entity.getBillCode());
				return new ModelResult<>("workflow.notify.insert.error", "财务审核流程驳回通知失败");
			}
		} else if (notifyParam.getProcessStatus().equals(ProcessTaskMain.PROECESS_STATUS_REJECT)) {

		}
		return new ModelResult<Boolean>().withModel(Boolean.TRUE);
	}
	
}
